package sg.edu.nus.iss.vmcs.customer;

import sg.edu.nus.iss.vmcs.util.LabelledDisplay;
import sg.edu.nus.iss.vmcs.util.VMCSException;
import sg.edu.nus.iss.vmcs.store.*;
import sg.edu.nus.iss.vmcs.machinery.MachineryController;

public class DispenseController {

	private TransactionController tCtrl;
	
	public DispenseController(TransactionController tCtrl){
		this.tCtrl=tCtrl;		
	}
	
	public TransactionController getTransactionController() {
		return tCtrl;
	}
	
	public Boolean dispenseDrink(int selected_brand) throws VMCSException {
		//Boolean result=true;
		LabelledDisplay collectCan=null;
		MachineryController machineryController=null;
		StoreController storeController=null;
        DrinksStoreItem drinkStoreItem;
        DrinksBrand drinkBrand;

        //to get the drink can and display
        machineryController= tCtrl.getMainController().getMachineryController();
        machineryController.dispenseDrink(selected_brand);
        collectCan = tCtrl.getCustomerPanel().getCollectCanDisplay();
      
        //to update the drink store display in Machinery Panel
         storeController= tCtrl.getMainController().getStoreController();
        
        // to get the drink brand and update drink
        drinkStoreItem = (DrinksStoreItem)storeController.getStoreItem(Store.DRINK,selected_brand);
        drinkBrand = (DrinksBrand)drinkStoreItem.getContent();        
        collectCan.setValue(drinkBrand.getName());
        updateDrinkSelection(selected_brand);
      
        return true;
        
	}
	
	public void updateDrinkPanel(){
		StoreController storeController= tCtrl.getMainController().getStoreController();
		int drinkstore = storeController.getStoreSize(Store.DRINK);
		DrinksStore d=(DrinksStore)tCtrl.getMainController().getStoreController().getStore(Store.DRINK);

		for(int i=0;i<drinkstore;i++){
	
			Integer stock=d.getStoreItem(i).getQuantity();			
			Boolean active=false;
			if(stock>0){
				active=true;
				updateDrinkSelection(i);
			}
			
			tCtrl.getCustomerPanel().getDrinkSelectionBox().setState(i,active);
			tCtrl.getCustomerPanel().getDrinkSelectionBox().setItemState(i,active);
		
		}
		
	}
	

	
	public void allowSelection(Boolean allow){
		if(tCtrl.getCustomerPanel() != null) {
			tCtrl.getCustomerPanel().getDrinkSelectionBox().setActive(allow);
			if(allow==true) //If active, update DrinkPanel
			{
				updateDrinkPanel();
			}
		}
	}
	
	public void resetCan(){
		CustomerPanel custPanel=tCtrl.getCustomerPanel();
		LabelledDisplay lblDisplay=custPanel.getCollectCanDisplay();
		lblDisplay.setValue("");	
	}
	
	public void updateDrinkSelection(int Idx){
        DrinkSelectionBox drinkSelectionBox= tCtrl.getCustomerPanel().getDrinkSelectionBox();
        StoreController storeController= tCtrl.getMainController().getStoreController();;
        DrinksStoreItem drinkStoreItem;
        DrinksBrand drinkBrand;

        drinkStoreItem = (DrinksStoreItem)storeController.getStoreItem(Store.DRINK,Idx);
        drinkBrand = (DrinksBrand)drinkStoreItem.getContent();

        drinkSelectionBox.update(Idx, drinkStoreItem.getQuantity(), drinkBrand.getPrice(), drinkBrand.getName());
    }
	
}
